$(function () {
    $("body").on('click','#cash-btn',function () {//申请提现
        var usableAmount = $.trim($("#usable-amount").text());
        var accountList = getBindingAccount();
        if(accountList==null || accountList==''){
            icModale(2,'','未绑定任何提现账户，是否去绑定？','去绑定提现账号','',function (index,elm) {
                layer.close(index);
                window.location.href = baseloaction +'/web/uc/account/myCash';
            });
        }else{
            var context = '<div class="mt20 ml20 mr20">';
            context +='<div class="form-horizontal">';
            context +='     <div class="form-group">';
            context +='            <label class="col-sm-3 control-label text-right unFw" style="padding-top: 0">提现账号</label>';
            context +='             <div class="col-sm-8">';
            context +='                 <select id="account" class="form-control">';
            $.each(accountList,function (index,account) {
                if(account.accountType==1){
                    context +='                     <option value="'+account.account+'" data-type="1">'+account.account+'(支付宝)</option>';
                }
                if(account.accountType==2){
                    context +='                     <option value="'+account.account+'" data-type="2">'+account.account+'(微信)</option>';
                }
            });
            context +='                 </select>';
            context +='                 <p class="mt5"><span class="fs12 c-ccc">请选择提现账号</span></p>';
            context +='             </div>';
            context +='     </div>';
            context +='     <div class="form-group">';
            context +='            <label class="col-sm-3 control-label text-right unFw" style="padding-top: 0">提现金额</label>';
            context +='             <div class="col-sm-8">';
            context +='                 <input placeholder="请输入提现金额" class="form-control" maxlength="4" onblur="checkPrice(this)" id="price" value="" type="text"/>';
            context +='                 <p class="mt5"><span class="fs12 c-ccc">请输入要提现的金额</span></p>';
            context +='             </div>';
            context +='     </div>';
            context +='</div>';
            context +='</div>';
            icModale(2,'<strong>提现操作</strong>',context,'确认','520px',function(index, ele){
                var account = ele.find('#account').val();
                var accountType = ele.find("#account :selected").data('type');
                var price = ele.find('#price').val();
                if(price==''){
                    icModale(0,'','请输入要提现的金额');
                    return;
                }
                //保存提现记录
                saveCashRecord(account,accountType,price);
            });
        }
    });

    /**
     * 保存提现记录
     * @param account 账户
     * @param accountType 类型
     * @param price 提现金额
     */
    function saveCashRecord(account,accountType,price) {
        $.ajax({
            url: baseloaction +'/web/uc/cash/saveCashRecord',
            type:'post',
            dataType:'json',
            data:{
                'account':account,
                'accountType':accountType,
                'price':price
            },
            success:function (result) {
                if(result.success==true){
                    icModale(1, '', result.message, '', null, function () {
                        window.location.href = baseloaction +'/web/uc/myAccount';
                    });
                }else{
                    icModale(0,'',result.message);
                }
            },
            error:function (error) {
                icModale(0,'','系统错误，提现申请失败');
            }
        });
    }

    /**
     * 获取绑定提现账户
     */
    function getBindingAccount() {
        var accountList = null;
        $.ajax({
            url: baseloaction +'/web/uc/account/findUserCashAccountList',
            type:'post',
            dataType:'json',
            async:false,
            success:function (result) {
                accountList = result.entity;
            }
        });
        return accountList;
    }
});

/**
 * 验证输入的金额
 * @param em
 */
function checkPrice(em) {
    var price = $(em).val();
    var priceReg = /^[1-9]+[0-9]*$/;
    if(!priceReg.test(price)){
        $(em).val('');
        return;
    }
    price = parseInt(price);
    if(price > 1000){
        $(em).val('1000');
        icModale(0,'','每次最可提现1000');
    }
}